package com.party.web.biz.crowdfundMultiple;

import com.google.common.base.Strings;
import com.party.common.utils.BigDecimalUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.activity.ActivityDetail;
import com.party.core.model.activity.ActivityInput;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.crowdfund.EventGradation;
import com.party.core.model.member.Member;
import com.party.core.model.partner.MemberPartner;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.activity.biz.ActivityDetailBizService;
import com.party.core.service.activity.biz.CrowfundResourceBizService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.biz.EventGradationBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.partner.IMemberPartnerService;
import com.party.web.utils.RealmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CrowdfundEventBizService
 * @Description 主题业务处理
 * @Author yifeng
 * @Date 2019/3/20 11:30
 **/
@Service
public class EventMultipleBizService {
    @Autowired
    private IMemberPartnerService memberPartnerService;
    @Autowired
    private ICrowdfundEventService crowdfundEventService;
    @Autowired
    private ActivityDetailBizService activityDetailBizService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private CrowfundResourceBizService crowfundResourceBizService;
    @Autowired
    private EventGradationBizService eventGradationBizService;
    @Autowired
    private IMemberGroupService memberGroupService;


    /**
     * @param event          主题
     * @param activityDetail 活动详情
     * @param activityInput
     * @param multipleActStr 多场活动字符串
     * @return void
     * @Author yifeng
     * @Description 主题保存
     * @Date 13:57 2019/3/20
     **/
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void eventSave(CrowdfundEvent event, ActivityDetail activityDetail,
                          ActivityInput activityInput, String multipleActStr) throws Exception {
        List<EventGradation> eventGradations = eventGradationBizService.transfrom(multipleActStr);
        Collections.reverse(eventGradations);
        if (eventGradations.size() > 0) {
            event.setEndTime(eventGradations.get(0).getEndTime());
        }
        event.setSwitchSupportRange(YesNoStatus.YES.getCode()); // 默认金额

        String memberId = RealmUtils.getCurrentUser().getId();
        String partnerId = memberGroupService.getPartnerId(memberId);
        MemberPartner memberPartner = memberPartnerService.findByMemberId(partnerId);
        if (Strings.isNullOrEmpty(event.getId())) {
            Double totalPrice = 0d;
            for (EventGradation eventGradation : eventGradations) {
                totalPrice = BigDecimalUtils.add(eventGradation.getPrice(), totalPrice);
            }
            Double round = BigDecimalUtils.round(totalPrice, 2);
            event.setPrice(round.floatValue());
            event.setCreateBy(memberId);
            event.setUpdateBy(memberId);
            event.setCrowdfundHintSwitch(YesNoStatus.NO.getCode());
            event.setIsMultiple(YesNoStatus.YES.getCode());

            //如果是系统用户，则将该主题的审核状态置为待审核，否则置为审核通过
            if (memberPartner != null) {
                event.setCheckStatus(0);
            }
            crowdfundEventService.insert(event);
        } else {
            if (memberPartner != null && event.getCheckStatus() == 2) {
                event.setCheckStatus(0);
            }
            event.setUpdateDate(new Date());
            crowdfundEventService.update(event);

            CrowdfundEvent db = crowdfundEventService.get(event.getId());
            memberId = db.getCreateBy();

            Activity activity = new Activity();
            activity.setEventId(event.getId());
            List<Activity> activities = activityService.list(activity);
            for (Activity act : activities) {
                act.setEndTime(event.getEndTime()); // 截止时间
                act.setLimitNum(event.getLimitNum()); // 人数上限
                activityService.update(act);
                activityDetailBizService.saveBizEvent(act.getId(), activityDetail);
            }
        }

        /************* 详情 ****************/
        activityDetailBizService.saveBiz(event.getId(), activityDetail);
        crowfundResourceBizService.saveResourceBiz(event.getId(), activityInput.getPicList(), activityInput.getVideoList(), memberId);
        eventGradationBizService.saveOrUpdateEventGradation(eventGradations, event.getId());
    }
}
